﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.Outlook;
using ToodleDo;
using Toodledo.Model;

namespace Sync.Outlook
{
    public static class TasksItems
    {
        #region Members
        public static Items All { get { return Local.Tasks; } }
        public static List<TaskItem> New
        {
            get
            {
                return All.Cast<TaskItem>().Where(item => item.GetId() <= 0).ToList();
            }
        }
        #endregion

        #region Constructors & Init

        #endregion

        #region Public Methods
        public static bool HasTaskWithCategory(Category c)
        {
            return (from TaskItem task in Local.Tasks
                    where task.Categories != null
                    select task.Categories
                    into categoryset select ((String) categoryset).Split(';')).Any(categories => categories[0] == c.Name);
        }

        public static TaskItem Add(Task task)
        {
            TaskItem taskItem = Local.Tasks.Add(OlItemType.olTaskItem);
            
            if (taskItem != null)
            {
                ApplyTaskDetails(taskItem,task);
                
                //todo add other specifics like duedate etc                
                //add id property
                taskItem.SetId(task.Id);            
                taskItem.Save();                
            }
            return taskItem;
        }

        public static void Update(this TaskItem taskItem, Task task)
        {
            if (taskItem != null)
            {
                ApplyTaskDetails(taskItem, task);
                //add id property             
                taskItem.Save();
            }
        }

        public static IEnumerable<TaskItem> NewTasksSince(DateTime date)
        {
            return All.Cast<TaskItem>().Where(item => item.CreationTime > date).ToList();
        }

        public static bool Contains(int id)
        {
            foreach (TaskItem item in All)
            {
                if (item.GetId() == id) return true;
            }
            return false;
        }

        public static TaskItem GetTaskItem(int id)
        {
            foreach (TaskItem item in All)
            {
                if (item.GetId() == id) return item;
            }
            return null;
        }
        #endregion

        #region #Events & Invokables

        #endregion

        #region Private Methods
        private static void ApplyTaskDetails(TaskItem taskItem, Task task)
        {
            taskItem.Subject = task.Name;
            if (task.Folder != null&&task.Folder.Id!=0)
            {
                taskItem.Categories = SyncSessionWrapper.SyncSession.GetFolder(task.Folder.Id).Name;
            }
            taskItem.DueDate = task.Due == new DateTime(1970, 1, 1, 0, 0, 0) ? new DateTime(4501, 1, 1, 0, 0, 0) : task.Due;
            taskItem.StartDate = task.Start == new DateTime(1970, 1, 1, 0, 0, 0) ? new DateTime(4501,1,1,0,0,0) : task.Start;
            switch (task.Priority)
            {
                case Priority.Negative:
                case Priority.Low:
                    taskItem.Importance = OlImportance.olImportanceLow;                    
                    break;
                case Priority.Medium:
                    taskItem.Importance = OlImportance.olImportanceNormal;
                    break;
                case Priority.High:
                case Priority.Top:
                case Priority.Ultimate:                
                    taskItem.Importance = OlImportance.olImportanceHigh;
                    break;                
                default:
                    taskItem.Importance = OlImportance.olImportanceHigh;
                    break;
            }
            taskItem.DateCompleted = task.Completed == new DateTime(1970, 1, 1, 0, 0, 0) ? new DateTime(4501, 1, 1, 0, 0, 0) : task.Completed;
            taskItem.Body = task.Note;
        }

        #endregion

        #region EventHandlers

        #endregion

        
    }
}
